package config

import (
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/mitchellh/mapstructure"
	"idle/utils/configUtls"
	"log"
)

// 武器攻速修正.xlsx
type Sheet1Config struct {
	Sid        int `json:"Sid"`        // Sid
	WeaponType int `json:"WeaponType"` // 武器类型
	HandType   int `json:"handType"`   // 单手双手
	Job        int `json:"Job"`        // 职业
	FixValue   int `json:"FixValue"`   // 修正
}

var sheet1ConfigList []*Sheet1Config
var sheet1ConfigMap map[int]*Sheet1Config

func GetSheet1Config(Sid int) *Sheet1Config {
	if v, ok := sheet1ConfigMap[Sid]; ok {
		return v
	}
	return nil
}

func GetAllSheet1Config() []*Sheet1Config {
	result := make([]*Sheet1Config, 0)
	for _, c := range sheet1ConfigList {
		result = append(result, c)
	}
	return result
}

func loadSheet1Config(filePath string) {
	xlsx, err := excelize.OpenFile(filePath)
	if err != nil {
		log.Fatal(err)
	}
	sheetName := xlsx.GetSheetName(1)
	rows := xlsx.GetRows(sheetName)

	sheet1ConfigMap = make(map[int]*Sheet1Config)
	sheet1ConfigList = make([]*Sheet1Config, 0)

	for _, row := range rows[4:] {
		data := make(map[string]interface{})
		for j, colCell := range row {
			fieldName := rows[1][j]
			if fieldName != "" {
				data[rows[1][j]] = configUtls.GetValueByType(rows[2][j], colCell)
			}
		}
		var config Sheet1Config
		err := mapstructure.Decode(data, &config)
		if err != nil {
			log.Fatal(err)
			return
		}

		sheet1ConfigMap[config.Sid] = &config
		sheet1ConfigList = append(sheet1ConfigList, &config)
	}
}
